import MComponent from '../../common/MComponent'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import { store } from '../../store/index'
import { _list as _activitylist } from '../../api/activity'
import { _goods as _goodslist } from '../../api/home'
import { _list as _newslist } from '../../api/news'
import { _projectlist } from '../../api/projects'
import { _bannber } from '../../api/home'
import { _ads } from '../../api/home'
const app = getApp()
MComponent({
  behaviors: [storeBindingsBehavior],
  storeBindings: {
    store,
    fields: {
      city: store => store.city
    }
  },
  data: {
    banners: [],
    projects: [],
    acts: [],
    news: [],
    goods: [],
    ads: [],
    current: 0,
    adShow: false,
    ignore: false,
    max: 3
  },
  methods: {
    getAd () {
      const { max } = this.data
      const lasttime = wx.getStorageSync('lasttime')
      const now = Date.now()
      if (lasttime && (now - lasttime) < max*24*60*60*1000) {
        return
      }
      const { city } = this.data
      const CityID = city.ID
      _ads({ CityID })
        .then(res => {
          const { code, msg, data } = res.data
          if (code === 0) {
            this.set({
              ads: data
            })
              .then(() => {
                if (data.length > 0) {
                  this.set({
                    adShow: true,
                    ignore: false
                  })
                }
              })
          } else {
            console.log(msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    getBanner () {
      const { city } = this.data
      const CityID = city.ID
      _bannber({ CityID })
        .then(res => {
          const { code, msg, data } = res.data
          if (code === 0) {
            this.set({
              topCurrent: 0,
              banners: data
            })
          } else {
            console.log(msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    getProjects() {
      app.loading('加载中')
      const { city } = this.data
      // const CityID = city.ID
      const CityID = city.ID ? [city.ID] : []
      _projectlist({ CityID, PageIndex: 1, PageSize: 10 })
        .then(res => {
          wx.hideLoading()
          const { code, msg, data } = res.data
          if (code === 0) {
            let list = data.List
            list = list.map(item => {
              item.tags = item.TagList ? JSON.parse(item.TagList) : []
              return item
            })
            this.set({
              projects: list
            })
          } else {
            console.log(msg)
          }
        })
        .catch(err => {
          wx.hideLoading()
          console.log(err)
        })
    },
    getActs() {
      const { city } = this.data
      const CityID = city.ID
      _activitylist({ CityID, PageIndex: 1, PageSize: 2 })
        .then(res => {
          const { code, msg, data } = res.data
          if (code === 0) {
            this.set({
              acts: data.List
            })
          } else {
            console.log(msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    getNews() {
      const { city } = this.data
      const CityID = city.ID
      _newslist({ CityID, Type: '', PageIndex: 1, PageSize: 4 })
        .then(res => {
          const { code, msg, data } = res.data
          if (code === 0) {
            this.set({
              news: data.List
            })
          } else {
            console.log(msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    getGoods () {
      const { city } = this.data
      const CityID = city.ID
      _goodslist({ CityID })
        .then(res => {
          const { code, msg, data } = res.data
          if (code === 0) {
            this.set({
              goods: data
            })
          } else {
            console.log(msg)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    onChange(e) {
      const { current } = e.detail
      this.set({
        current
      })
    },
    hideAd () {
      const { ignore } = this.data
      if (ignore) {
        wx.setStorageSync('lasttime', Date.now())
      } else {
        wx.removeStorageSync('lasttime')
      }
      this.set({
        adShow: false
      })
    },
    toggle () {
      this.set({
        ignore: !this.data.ignore
      })
    },
    onInit (e) {
      this.data.city = e.detail.value
      this.getBanner()
      this.getProjects()
      this.getActs()
      this.getNews()
      this.getGoods()
      this.getAd()
      wx.hideNavigationBarLoading()
    },
    onLoad() {},
    onShow () {
      this.data.city = store.city
      if (this.data.city) {
        this.getBanner()
        this.getProjects()
        this.getActs()
        this.getNews()
        this.getGoods()
        this.getAd()
        wx.hideNavigationBarLoading() 
      }
    }
  }
}, true)
